package net.qiqb.subclass;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.utility.JavaModule;

import java.security.ProtectionDomain;

import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy;

public class MainTest2 {
    public static void main(String[] args) {
        // Install ByteBuddyAgent
        ByteBuddyAgent.install();

        // Define the transformation logic using AgentBuilder
        new AgentBuilder.Default()
                .type(ElementMatchers.named("net.qiqb.subclass.MainTest2.Order"))
                .transform(new AgentBuilder.Transformer() {
                    @Override
                    public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule, ProtectionDomain protectionDomain) {

                        builder.method(isDeclaredBy(Object.class))
                                .intercept(Advice.to(TranslatePostConstruct.class));
                        return builder;
                    }
                })
                .installOn(ByteBuddyAgent.getInstrumentation());

        // Create an instance of the original Order class
        Order order = new Order();
        order.process(); // This should print "Processing SubOrder"
    }

    public static class Order {
        public void process() {
            System.out.println("Processing Order");
        }
    }

    public static class SubOrder extends Order {
        @Override
        public void process() {
            System.out.println("Processing SubOrder");
        }
    }
}